package com.itheima.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class JedisUtils {
private static JedisPool jedisPool;
static{
    Properties properties = new Properties();
    InputStream is = JedisUtils.class.getClassLoader().getResourceAsStream("redis.properties");
    try {
        properties.load(is);
    } catch (IOException e) {
        e.printStackTrace();
    }
    String host = properties.getProperty("redis.host");
    int port = Integer.parseInt(properties.getProperty("redis.port"));
    String password = properties.getProperty("redis.auth");
    int timeout = Integer.parseInt(properties.getProperty("redis.timeout"));
    int maxTotal = Integer.parseInt(properties.getProperty("redis.maxActive"));
    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
    jedisPoolConfig.setMaxTotal(maxTotal);
    jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);
}
    public static Jedis getJedis(){
    return jedisPool.getResource();
    }
    public static void close(Jedis jedis){
    if (jedis!=null){
        jedis.close();
    }
    }
}
